package document

import (
	"fmt"

	"github.com/sirupsen/logrus"
)

// FileInfoService 文件信息服务
type FileInfoService struct {
	dao *FileInfoDAO
}

// NewFileInfoService 创建文件信息服务实例
func NewFileInfoService(dao *FileInfoDAO) *FileInfoService {
	return &FileInfoService{dao: dao}
}

// CreateFileInfo 创建文件信息
func (s *FileInfoService) CreateFileInfo(req *CreateFileInfoRequest) (*FileInfoResponse, error) {
	// 检查文件名是否已存在
	existing, _ := s.dao.GetByFileName(req.FileName)
	if existing != nil {
		return nil, fmt.Errorf("文件名已存在")
	}

	fileInfo := &FileInfo{
		FileName:        req.FileName,
		FileDescription: req.FileDescription,
		Remark:          req.Remark,
		FileAuthor:      req.FileAuthor,
		S3Links:         req.S3Links,
	}

	err := s.dao.Create(fileInfo)
	if err != nil {
		logrus.Errorf("创建文件信息失败: %v", err)
		return nil, fmt.Errorf("创建文件信息失败: %w", err)
	}

	return &FileInfoResponse{
		ID:              fileInfo.ID,
		FileName:        fileInfo.FileName,
		FileDescription: fileInfo.FileDescription,
		Remark:          fileInfo.Remark,
		FileAuthor:      fileInfo.FileAuthor,
		S3Links:         fileInfo.S3Links,
		CreatedAt:       fileInfo.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt:       fileInfo.UpdatedAt.Format("2006-01-02 15:04:05"),
	}, nil
}

// GetFileInfo 获取文件信息
func (s *FileInfoService) GetFileInfo(id int) (*FileInfoResponse, error) {
	fileInfo, err := s.dao.GetByID(id)
	if err != nil {
		return nil, err
	}

	return &FileInfoResponse{
		ID:              fileInfo.ID,
		FileName:        fileInfo.FileName,
		FileDescription: fileInfo.FileDescription,
		Remark:          fileInfo.Remark,
		FileAuthor:      fileInfo.FileAuthor,
		S3Links:         fileInfo.S3Links,
		CreatedAt:       fileInfo.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt:       fileInfo.UpdatedAt.Format("2006-01-02 15:04:05"),
	}, nil
}

// UpdateFileInfo 更新文件信息
func (s *FileInfoService) UpdateFileInfo(req *UpdateFileInfoRequest) (*FileInfoResponse, error) {
	fileInfo, err := s.dao.GetByID(req.ID)
	if err != nil {
		return nil, err
	}

	// 更新字段
	fileInfo.FileName = req.FileName
	fileInfo.FileDescription = req.FileDescription
	fileInfo.Remark = req.Remark
	fileInfo.FileAuthor = req.FileAuthor
	fileInfo.S3Links = req.S3Links

	err = s.dao.Update(fileInfo)
	if err != nil {
		logrus.Errorf("更新文件信息失败: %v", err)
		return nil, fmt.Errorf("更新文件信息失败: %w", err)
	}

	return &FileInfoResponse{
		ID:              fileInfo.ID,
		FileName:        fileInfo.FileName,
		FileDescription: fileInfo.FileDescription,
		Remark:          fileInfo.Remark,
		FileAuthor:      fileInfo.FileAuthor,
		CreatedAt:       fileInfo.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt:       fileInfo.UpdatedAt.Format("2006-01-02 15:04:05"),
	}, nil
}

// DeleteFileInfo 删除文件信息
func (s *FileInfoService) DeleteFileInfo(id int) error {
	return s.dao.Delete(id)
}

// DeleteFileInfoByName 根据文件名删除文件信息
func (s *FileInfoService) DeleteFileInfoByName(fileName string) error {
	// 先根据文件名查询文件信息
	fileInfo, err := s.dao.GetByFileName(fileName)
	if err != nil {
		return fmt.Errorf("文件不存在: %w", err)
	}
	
	// 删除文件信息
	return s.dao.Delete(fileInfo.ID)
}

// ListFileInfos 获取文件信息列表
func (s *FileInfoService) ListFileInfos(req *ListFileInfoRequest) (*ListFileInfoResponse, error) {
	fileInfos, total, err := s.dao.List(req.PageNum, req.PageSize, req.FileName, req.FileAuthor)
	if err != nil {
		return nil, fmt.Errorf("获取文件信息列表失败: %w", err)
	}

	var responses []*FileInfoResponse
	for _, fileInfo := range fileInfos {
		responses = append(responses, &FileInfoResponse{
			ID:              fileInfo.ID,
			FileName:        fileInfo.FileName,
			FileDescription: fileInfo.FileDescription,
			Remark:          fileInfo.Remark,
			FileAuthor:      fileInfo.FileAuthor,
			S3Links:         fileInfo.S3Links,
			CreatedAt:       fileInfo.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdatedAt:       fileInfo.UpdatedAt.Format("2006-01-02 15:04:05"),
		})
	}

	return &ListFileInfoResponse{
		List:     responses,
		Total:    total,
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}, nil
}
